Add cache_dir_fd to OstreeRepo
authorAlexander Larsson <alexl@redhat.com>
Mon, 11 Apr 2016 08:25:40 +0000 (10:25 +0200)
committerColin Walters (automation) <walters+githubbot@verbum.org>
Thu, 14 Apr 2016 15:55:08 +0000 (15:55 +0000)
This will allow us later to easily swap out the cache dir.

Closes: #250
Approved by: cgwalters

src/libostree/ostree-repo-private.h
src/libostree/ostree-repo-prune.c
src/libostree/ostree-repo-pull.c
src/libostree/ostree-repo.c

index c60ab2463960900fa55c4471191e700d247285a2..1cf99ead4e7b4ffa11c1ca98c09e08b1f64a3a4c 100644 (file)
@@ -33,7 +33,8 @@ G_BEGIN_DECLS
 
 #define _OSTREE_OBJECT_SIZES_ENTRY_SIGNATURE "ay"
 
-#define _OSTREE_SUMMARY_CACHE_PATH "tmp/cache/summaries"
+#define _OSTREE_SUMMARY_CACHE_DIR "summaries"
+#define _OSTREE_CACHE_DIR "cache"
 
 /**
  * OstreeRepo:
@@ -52,6 +53,7 @@ struct OstreeRepo {
   int    repo_dir_fd;
   GFile *tmp_dir;
   int    tmp_dir_fd;
+  int    cache_dir_fd;
   GFile *objects_dir;
   GFile *state_dir;
   int objects_dir_fd;
index 9aed0ac388a66f8fc799e34a8b4181ed5ca19c23..8c5d13e97dc9a81c46fdc1e78d600e464c8c9d2c 100644 (file)
@@ -125,7 +125,10 @@ _ostree_repo_prune_tmp (OstreeRepo *self,
   g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
   glnx_fd_close int fd = -1;
 
-  fd = glnx_opendirat_with_errno (self->repo_dir_fd, _OSTREE_SUMMARY_CACHE_PATH, FALSE);
+  if (self->cache_dir_fd == -1)
+    return TRUE;
+
+  fd = glnx_opendirat_with_errno (self->cache_dir_fd, _OSTREE_SUMMARY_CACHE_DIR, FALSE);
   if (fd < 0)
     {
       if (errno == ENOENT)
index f292951e227532c1787a1737a976eb091d6b2a36..eef5f039979bd74cb1881d0668437ec324444730 100644 (file)
@@ -1783,12 +1783,15 @@ _ostree_repo_load_cache_summary_if_same_sig (OstreeRepo        *self,
                                              GError           **error)
 {
   gboolean ret = FALSE;
-  const char *summary_cache_sig_file = glnx_strjoina (_OSTREE_SUMMARY_CACHE_PATH, "/", remote, ".sig");
+  const char *summary_cache_sig_file = glnx_strjoina (_OSTREE_SUMMARY_CACHE_DIR, "/", remote, ".sig");
 
   glnx_fd_close int prev_fd = -1;
   g_autoptr(GBytes) old_sig_contents = NULL;
 
-  if (!ot_openat_ignore_enoent (self->repo_dir_fd, summary_cache_sig_file, &prev_fd, error))
+  if (self->cache_dir_fd == -1)
+    return TRUE;
+
+  if (!ot_openat_ignore_enoent (self->cache_dir_fd, summary_cache_sig_file, &prev_fd, error))
     goto out;
 
   if (prev_fd < 0)
@@ -1803,17 +1806,17 @@ _ostree_repo_load_cache_summary_if_same_sig (OstreeRepo        *self,
 
   if (g_bytes_compare (old_sig_contents, summary_sig) == 0)
     {
-      const char *summary_cache_file = glnx_strjoina (_OSTREE_SUMMARY_CACHE_PATH, "/", remote);
+      const char *summary_cache_file = glnx_strjoina (_OSTREE_SUMMARY_CACHE_DIR, "/", remote);
       glnx_fd_close int summary_fd = -1;
       GBytes *summary_data;
 
 
-      summary_fd = openat (self->repo_dir_fd, summary_cache_file, O_CLOEXEC | O_RDONLY);
+      summary_fd = openat (self->cache_dir_fd, summary_cache_file, O_CLOEXEC | O_RDONLY);
       if (summary_fd < 0)
         {
           if (errno == ENOENT)
             {
-              (void) unlinkat (self->repo_dir_fd, summary_cache_sig_file, 0);
+              (void) unlinkat (self->cache_dir_fd, summary_cache_sig_file, 0);
               ret = TRUE;
               goto out;
             }
@@ -1842,13 +1845,16 @@ _ostree_repo_cache_summary (OstreeRepo        *self,
                             GError           **error)
 {
   gboolean ret = FALSE;
-  const char *summary_cache_file = glnx_strjoina (_OSTREE_SUMMARY_CACHE_PATH, "/", remote);
-  const char *summary_cache_sig_file = glnx_strjoina (_OSTREE_SUMMARY_CACHE_PATH, "/", remote, ".sig");
+  const char *summary_cache_file = glnx_strjoina (_OSTREE_SUMMARY_CACHE_DIR, "/", remote);
+  const char *summary_cache_sig_file = glnx_strjoina (_OSTREE_SUMMARY_CACHE_DIR, "/", remote, ".sig");
+
+  if (self->cache_dir_fd == -1)
+    return TRUE;
 
-  if (!glnx_shutil_mkdir_p_at (self->repo_dir_fd, _OSTREE_SUMMARY_CACHE_PATH, 0775, cancellable, error))
+  if (!glnx_shutil_mkdir_p_at (self->cache_dir_fd, _OSTREE_SUMMARY_CACHE_DIR, 0775, cancellable, error))
     goto out;
 
-  if (!glnx_file_replace_contents_at (self->repo_dir_fd,
+  if (!glnx_file_replace_contents_at (self->cache_dir_fd,
                                       summary_cache_file,
                                       g_bytes_get_data (summary, NULL),
                                       g_bytes_get_size (summary),
@@ -1856,7 +1862,7 @@ _ostree_repo_cache_summary (OstreeRepo        *self,
                                       cancellable, error))
     goto out;
 
-  if (!glnx_file_replace_contents_at (self->repo_dir_fd,
+  if (!glnx_file_replace_contents_at (self->cache_dir_fd,
                                       summary_cache_sig_file,
                                       g_bytes_get_data (summary_sig, NULL),
                                       g_bytes_get_size (summary_sig),
index d59c8e7809b6b9233617209bccac233c4549d208..b8cb242594627d5133b2146a4aac010f62fb435c 100644 (file)
@@ -613,6 +613,8 @@ ostree_repo_finalize (GObject *object)
   g_clear_object (&self->tmp_dir);
   if (self->tmp_dir_fd)
     (void) close (self->tmp_dir_fd);
+  if (self->cache_dir_fd)
+    (void) close (self->cache_dir_fd);
   g_clear_object (&self->objects_dir);
   if (self->objects_dir_fd != -1)
     (void) close (self->objects_dir_fd);
@@ -784,6 +786,7 @@ ostree_repo_init (OstreeRepo *self)
   g_mutex_init (&self->remotes_lock);
 
   self->repo_dir_fd = -1;
+  self->cache_dir_fd = -1;
   self->commit_stagedir_fd = -1;
   self->objects_dir_fd = -1;
   self->uncompressed_objects_dir_fd = -1;
@@ -2371,6 +2374,7 @@ ostree_repo_open (OstreeRepo    *self,
   g_autofree char *version = NULL;
   g_autofree char *mode = NULL;
   g_autofree char *parent_repo_path = NULL;
+  g_autoptr(GError) temp_error = NULL;
 
   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
@@ -2507,6 +2511,34 @@ ostree_repo_open (OstreeRepo    *self,
   if (!glnx_opendirat (self->repo_dir_fd, "tmp", TRUE, &self->tmp_dir_fd, error))
     goto out;
 
+  if (!glnx_shutil_mkdir_p_at (self->tmp_dir_fd, _OSTREE_CACHE_DIR, 0775, cancellable, &temp_error))
+    {
+      if (g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED))
+        {
+          g_clear_error (&temp_error);
+          g_debug ("No permissions to create cache dir");
+        }
+      else
+        {
+          g_propagate_error (error, g_steal_pointer (&temp_error));
+          goto out;
+        }
+    }
+
+  if (!glnx_opendirat (self->tmp_dir_fd, _OSTREE_CACHE_DIR, TRUE, &self->cache_dir_fd, &temp_error))
+    {
+      if (g_error_matches (temp_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
+        {
+          g_clear_error (&temp_error);
+          g_debug ("No cache dir");
+        }
+      else
+        {
+          g_propagate_error (error, g_steal_pointer (&temp_error));
+          goto out;
+        }
+    }
+
   if (self->mode == OSTREE_REPO_MODE_ARCHIVE_Z2 && self->enable_uncompressed_cache)
     {
       if (!gs_file_ensure_directory (self->uncompressed_objects_dir, TRUE, cancellable, error))